/// @Author wywinstonwy
/// @Date 2022/1/16 10:15 上午
/// @Description: 
import "package:flutter/material.dart";

class AnimatedDecoratedBox1 extends StatefulWidget {
  final BoxDecoration decoration;
  final Widget child;
  final Duration duration;
  final Curve ?curve;
  final Duration? reverseDuration;

  const AnimatedDecoratedBox1({
    required this.decoration,
    required this.child,
    required this.duration,
     this.curve,
      this.reverseDuration});


  @override
  _AnimatedDecoratedBox1State createState() => _AnimatedDecoratedBox1State();
}

class _AnimatedDecoratedBox1State extends State<AnimatedDecoratedBox1> with SingleTickerProviderStateMixin{
  AnimationController get controller=>_controller;
  late AnimationController _controller;
  late Animation<double> _animation;
  late DecorationTween _tween;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(
      duration: widget.duration,
      reverseDuration: widget.reverseDuration,
      vsync: this
    );

    _tween = DecorationTween(begin: widget.decoration);
    _updateCurve();
  }
  void _updateCurve(){
    _animation = CurvedAnimation(parent: _controller, curve: widget.curve!);
  }
  @override
  void didUpdateWidget(covariant AnimatedDecoratedBox1 oldWidget) {
    super.didUpdateWidget(oldWidget);
    if(widget.curve != oldWidget) _updateCurve();
    _controller.duration = widget.duration;
    _controller.reverseDuration = widget.reverseDuration;
    //正在执行过度动画
    if(widget.decoration !=(_tween.end??_tween.begin)){
      _tween
      ..begin =_tween.evaluate(_animation)
      ..end = widget.decoration;

      _controller
      ..value=0.0
      ..forward();
    }


  }
  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      builder: (BuildContext context, Widget? child) {
        return DecoratedBox(

            decoration: _tween.animate(_animation).value);
      },
      animation: _animation,
      child:widget.child,
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();

  }

}

// //动画封装
// class AnimatedDecoratedBox extends ImplicitlyAnimatedWidget {
//   final Widget child;
//   final BoxDecoration decoration;
//
//   AnimatedDecoratedBox({
//     Key? key,
//     required this.child,
//     required this.decoration,
//     Curve curve = Curves.linear,
//     required Duration duration
//   }) : super(key: key,curve: curve,duration: duration);
//
//   @override
//   _AnimatedDecoratedBoxState createState() => _AnimatedDecoratedBoxState();
// }
//
// class _AnimatedDecoratedBoxState extends AnimatedWidgetBaseState<AnimatedDecoratedBox> {
//   var _decoration; //定义一个Tween
//   @override
//   Widget build(BuildContext context) {
//     return DecoratedBox(
//       decoration: _decoration.evaluate(animation),
//       child: widget.child,
//     );
//   }
//
//   @override
//   void forEachTween(TweenVisitor visitor) {
//     // TODO: implement forEachTween
//     _decoration = visitor(
//       _decoration,
//       widget.decoration,
//         (value)=>DecorationTween(begin: value),
//     );
//   }
// }




